package offer;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Jonny Long
 * @date 2021/3/11 20:55
 */
public class Solution03 {
    // 方法一，排序
    public int findRepeatNumber_01(int[] arr) {
        Arrays.sort(arr);
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] == arr[i + 1]) {
                return arr[i];
            }
        }
        return -1;
    }

    // 方法二，硬解
    public int findRepeatNumber_02(int[] arr) {
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (arr[i] == arr[j]) {
                    return arr[i];
                }
            }
        }
        return -1;
    }

    // 方法三，list集合
    public int findRepeatNumber_03(int[] arr) {
        LinkedList<Integer> list = new LinkedList<>();
        for(int num:arr){
            if (list.contains(num)){
                return num;
            }
            list.add(num);
        }
        return -1;
    }
}
